<?php
/**
 * Created by PhpStorm.
 * User: ZhangYi
 * Date: 2011-1-13
 * Time: 14:04:44
 * To change this template use File | Settings | File Templates.
 */

class BuildSearchIndexTask extends sfBaseTask
{
    protected function configure()
    {
        parent::configure();
        $this->addOptions(array(new sfCommandOption('target', null,
                              sfCommandOption::PARAMETER_REQUIRED, 'target of index building for search', 'All')));
        $this->namespace = 'ieasy';
        $this->name = 'buildIndex';
    }


    protected function execute($arguments = array(), $options = array())
    {
        try {

            $databaseManager = new sfDatabaseManager($this->configuration);


            switch ($options['target']) {
                case 'News':
                    $NewsIndex = IEasySearch::getLuceneIndex('News');
//                    print_r(Zend_Search_Lucene_Analysis_Analyzer::getDefault());

                    $NewsCollection = Doctrine::getTable('News')->findAll();

                    $updated = 0;
                    $total = 0;
                    foreach ($NewsCollection as $News) {
                        //Remove old index
                        foreach ($NewsIndex->find('id:' . $News->getNewsId()) as $hit) {
                            $NewsIndex->delete($hit->id);
                            $updated++;
                        }

                        //build new index
                        $doc = new Zend_Search_Lucene_Document();
                        $doc->addField(Zend_Search_Lucene_Field::keyword('id', $News->getNewsId(), 'utf-8'));
                        $doc->addField(Zend_Search_Lucene_Field::text('title', $News->getTitle(), 'utf-8'));
                        $doc->addField(Zend_Search_Lucene_Field::text('author', $News->getAuthor(), 'utf-8'));
                        $doc->addField(Zend_Search_Lucene_Field::unStored('body', IEasySearch::getContentWithOutHtmlTags($News->getBody()), 'utf-8'));
                        ;
                        $NewsIndex->addDocument($doc);
                        //                       echo  $doc->getFieldValue('body')."\n";
                        //                        echo $News->getBody()."\n";
                        $total++;
                    }
                    $NewsIndex->commit();
                    $NewsIndex->optimize();
                    echo "build Index success, updated Index count:" . $updated . ", total Index count:" . $total . ". \n";
                    break;
                case 'All':
                    //sfConfig::set('sf_environment', 'dev');
                    //$databaseManager = new sfDatabaseManager($this->configuration);
                    //$this->buildAll();

                    sfConfig::set('sf_environment', 'prod');
                    $databaseManager = new sfDatabaseManager($this->configuration);
                    $this->buildAll();
                default:
                    break;

            }


        } catch (Exception $e) {
            echo $e->getMessage();
            echo $e->getTraceAsString();
            $this->log($e->getMessage());
            $this->log($e->getTraceAsString());
        }
    }


    private function buildAll()
    {
        try {
            $NewsCollection = Doctrine::getTable('News')->createQuery('a')
                    ->addWhere('a.status_id = ?', array(1005, 1004))
                    ->addOrderBy('a.publish_time desc')->limit(100)
                    ->execute();
            echo count($NewsCollection) . "\n";

            $VideoCollection = Doctrine::getTable('Video')->createQuery('a')
                    ->addWhere('a.status_id = ?', array(1005, 1004))
                    ->addOrderBy('a.publish_time desc')->limit(100)
                    ->execute();

            echo count($VideoCollection) . "\n";

            $PhotoGroupCollection = Doctrine::getTable('PhotoGroup')->createQuery('a')
                    ->addWhere('a.status_id = ?', array(1005, 1004))
                    ->addOrderBy('a.publish_time desc')->limit(100)
                    ->execute();
            echo count($PhotoGroupCollection) . "\n";

            $generalIndex = IEasySearch::getLuceneIndex('All');

            //            $newsIndex = IEasySearch::getLuceneIndex('News');
            //            $videoIndex = IEasySearch::getLuceneIndex('Video');
            //            $photoGroupIndex = IEasySearch::getLuceneIndex('photoGroup');

            $updated = 0;
            $total = 0;
            echo "build News index: \n";
            foreach ($NewsCollection as $News)
            {

                foreach ($generalIndex->find('id:' . "news_" . $News->getNewsId()) as $hit) {
                    $generalIndex->delete($hit->id);
                    $updated++;
                }
                //build new index
                echo "building index news:" . $News->getNewsId() . "...";
                $doc = new Zend_Search_Lucene_Document();
                $doc->addField(Zend_Search_Lucene_Field::keyword('id', "news_" . $News->getNewsId(), 'utf-8'));
                $doc->addField(Zend_Search_Lucene_Field::keyword('publishTime', $News->getPublishTime(), 'utf-8'));
                $doc->addField(Zend_Search_Lucene_Field::text('keywords', $News->getKeyword(), 'utf-8'));
                $doc->addField(Zend_Search_Lucene_Field::text('title', $News->getTitle(), 'utf-8'));
                $doc->addField(Zend_Search_Lucene_Field::unStored('content', IEasySearch::getContentWithOutHtmlTags($News->getBody()), 'utf-8'));
                //                echo  IEasySearch::getContentWithOutHtmlTags($News->getBody()) . "\n";
                $generalIndex->addDocument($doc);
                $total++;
                echo " built success\n";
                //            $doc->addField(Zend_Search_Lucene_Field::text('author', $News->getAuthor(), 'utf-8'));
                //            $doc->addField(Zend_Search_Lucene_Field::unStored('body', $News->getBody(), 'utf-8'));
            }
            echo "News indexed: \n build video index \n";


            foreach ($VideoCollection as $video)
            {
                foreach ($generalIndex->find('id:' . "video_" . $video->getVideoId()) as $hit) {
                    $generalIndex->delete($hit->id);
                    $updated++;
                }

                $doc = new Zend_Search_Lucene_Document();
                $doc->addField(Zend_Search_Lucene_Field::keyword('id', "video_" . $video->getVideoId(), 'utf-8'));
                $doc->addField(Zend_Search_Lucene_Field::keyword('publishTime', $video->getPublishTime(), 'utf-8'));
                $keyword = $video->getKeyword() !== NULL ? $video->getKeyword() : "";
                $doc->addField(Zend_Search_Lucene_Field::text('keywords', $keyword, 'utf-8'));
                $doc->addField(Zend_Search_Lucene_Field::text('title', $video->getTitle(), 'utf-8'));
                $doc->addField(Zend_Search_Lucene_Field::text('artist', $video->getArtist(), 'utf-8'));
                $generalIndex->addDocument($doc);
                $total++;
            }

            echo "Video indexed: \n build photogroup index \n";
            foreach ($PhotoGroupCollection as $photoGroup)
            {
                foreach ($generalIndex->find('id:' . "pg_" . $photoGroup->getPhotoGroupId()) as $hit) {
                    $generalIndex->delete($hit->id);
                    $updated++;
                }
                $doc = new Zend_Search_Lucene_Document();
                $doc->addField(Zend_Search_Lucene_Field::keyword('id', "pg_" . $photoGroup->getPhotoGroupId(), 'utf-8'));
                $doc->addField(Zend_Search_Lucene_Field::keyword('publishTime', $photoGroup->getPublishTime(), 'utf-8'));
                $doc->addField(Zend_Search_Lucene_Field::text('keywords', $photoGroup->getKeyword(), 'utf-8'));
                $doc->addField(Zend_Search_Lucene_Field::text('title', $photoGroup->getTitle(), 'utf-8'));
                $doc->addField(Zend_Search_Lucene_Field::text('snapshot', $photoGroup->getSnapshot(), 'utf-8'));

                $generalIndex->addDocument($doc);
                $total++;
            }

            $generalIndex->commit();
            $generalIndex->optimize();
            echo "build Index success, updated Index count:" . $updated . ", total Index count:" . $total . ". \n";
        } catch (Exception $e)
        {
            //            echo $e->getMessage() . "\n";
            //            echo $e->getTraceAsString() . "\n";

        }
    }
}
